﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XmlStudio.DataLayer {
    /// <summary>
    /// Represents group of <see cref="XsdStructureItem"/>s.
    /// </summary>
    public class XsdStructureGroup : XsdStructureItem {
        private ICollection<XsdStructureItem> items = new List<XsdStructureItem>();

        /// <summary>
        /// Gets the collection of items in this group.
        /// </summary>
        public ICollection<XsdStructureItem> Items { get { return this.items; } }

        /// <summary>
        /// Gets or sets the operator which defines how these items are to be used.
        /// </summary>
        public GroupOperator Operator { get; set; }

        /// <summary>
        /// Overriden so two structure items are compared by their content.
        /// </summary>
        /// <param name="obj">Another instance to compare.</param>
        /// <returns>True if both instances have the same content, false otherwise.</returns>
        public override bool Equals(object obj) {
            if(!(obj is XsdStructureGroup))
                return false;

            if(obj == null)
                return false;

            XsdStructureGroup t = (XsdStructureGroup)obj;

            return (ElementType.Equals(t.ElementType) && Multiplicity.Equals(t.Multiplicity) && Operator.Equals(t.Operator) && Items.SequenceEqual(t.Items));
        }

        /// <summary>
        /// Returns the hashcode for the value of this instance.
        /// </summary>
        /// <returns>Hashcode.</returns>
        public override int GetHashCode() {
            return ElementType.GetHashCode() ^ Multiplicity.GetHashCode() ^ Operator.GetHashCode() ^ Items.GetHashCode();
        }
    }
}